package com.kitty.game.fight.artifact.passive;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.Skill;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.bean.SuperBossFightObject;
import com.kitty.game.fight.model.SkillAction;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.skill.model.RoleSkill;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
//番天印
public class StamperArtifactEffect extends ArtifactEffect {

    @Override
    public short getEffectId() {
        return 8013;
    }

    @Override
    public ArtifactType getType() {
        return ArtifactType.STAMPER;
    }

    @Override
    public Boolean trigger(Fight fight, FightObject member, Object... params) {
        /**超级boss另外判断*/
        if (member.isSuperBoss()) {
            return tirggerSuperBoss(fight, member, params);
        }

        if (!checkBaseCondition(fight, member)) {
            return false;
        }
        boolean trigger = SpringUtils.getArtifactService().getArtifactRate(getType().getName(), member.getRole());
        if (!trigger) {
            return false;
        }

        try {
            RoleSkill currSkill = (RoleSkill) params[0];
            // 同阶或低一阶的法攻技能
            List<Integer> targetSkillIds = new ArrayList<>();
            for (Map.Entry<Integer, RoleSkill> entry : member.getRole().getSkillBox().getSkills().entrySet()) {
                RoleSkill item = entry.getValue();
                Skill skill = SpringUtils.getSkillService().getSkillInfo(item.getSkillId());
                if (!skill.isMagicSkill()) {
                    continue;
                }
                if (skill.getJeishu() == currSkill.getSkillJieshu() || skill.getJeishu() == currSkill.getSkillJieshu() - 1) {
                    targetSkillIds.add(item.getSkillId());
                }
            }

            if (targetSkillIds.size() <= 0) {
                return false;
            }

            // 随机打乱技能列表
            Collections.shuffle(targetSkillIds);

            FightMessageService fightMessageService = SpringUtils.getFightMessageService();
            int nextSkill = targetSkillIds.get(0);
            member.setSkillId(nextSkill);

            RoleSkill roleSkill = member.getSkillBy(nextSkill);
            SkillAction skillAction = new SkillAction(fight, member, roleSkill);
            List<Integer> targets = skillAction.pickUpTargets();
            if (!CollectionUtils.isEmpty(targets)) {
                FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionStart(fight.getRound(), member));
                broadCast(fight, member);
                skillAction.doMagicEffect();
            }
        } catch (Exception e) {

        }
        return true;
    }

    /**
     * 超级boss是否触发法宝
     */
    protected Boolean tirggerSuperBoss(Fight fight, FightObject member, Object... params) {
        if (!checkSuperBossBaseCondition(fight, member)) {
            return false;
        }
        boolean trigger = SpringUtils.getArtifactService().getSuperBossArtifactRate(getType().getName(), (SuperBossFightObject) member);
        if (!trigger) {
            return false;
        }

        try {
            RoleSkill currSkill = (RoleSkill) params[0];
            // 同阶或低一阶的法攻技能
            List<Integer> targetSkillIds = new ArrayList<>();

            for (int skillId : member.getSkillIds()) {
                Skill skill = SpringUtils.getSkillService().getSkillInfo(skillId);
                if (skill == null || !skill.isMagicSkill()) {
                    continue;
                }
                if (skill.getJeishu() == currSkill.getSkillJieshu() || skill.getJeishu() == currSkill.getSkillJieshu() - 1) {
                    targetSkillIds.add(skillId);
                }
            }

            if (targetSkillIds.size() <= 0) {
                return false;
            }

            broadCast(fight, member);

            // 随机打乱技能列表
            Collections.shuffle(targetSkillIds);
            FightMessageService fightMessageService = SpringUtils.getFightMessageService();

            int nextSkill = targetSkillIds.get(0);
            member.setSkillId(nextSkill);

            FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionStart(fight.getRound(), member));

            RoleSkill roleSkill = member.getSkillBy(nextSkill);
            SkillAction skillAction = new SkillAction(fight, member, roleSkill);
            skillAction.pickUpTargets();
            skillAction.doMagicEffect();
        } catch (Exception e) {

        }
        return true;
    }
}
